package com.test.payproject.config;

import org.springframework.beans.factory.config.PropertiesFactoryBean;
import org.springframework.boot.context.event.ApplicationReadyEvent;
import org.springframework.context.ApplicationEvent;
import org.springframework.context.ApplicationListener;
import org.springframework.core.io.Resource;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
import org.springframework.stereotype.Component;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;

@Component
public class PropertiesConfig implements ApplicationListener {
    //用于保存加载配置参数
    private static Map<String,String> aliPropertiesMap = new HashMap<>();
    //监听启动完成，执行配置加载到aliPropertiesMap
    @Override
    public void onApplicationEvent(ApplicationEvent event) {
        if(event instanceof ApplicationReadyEvent){
            this.init(aliPropertiesMap);
        }
    }
    //获取配置参数值
    public static String getKey(String key){
        return aliPropertiesMap.get(key);
    }
    public void init(Map<String,String> map){

        // 获得PathMatchingResourcePatternResolver对象
        PathMatchingResourcePatternResolver resolver = new PathMatchingResourcePatternResolver();
        try {
            //加载resource文件(也可以加载resources)
            Resource resources = resolver.getResource("classpath:config/alipay.yml");
            PropertiesFactoryBean config = new PropertiesFactoryBean();
            config.setLocation(resources);
            config.afterPropertiesSet();
            Properties prop = config.getObject();
            //循环遍历所有得键值对并且存入集合
            for (String key : prop.stringPropertyNames()) {
                map.put(key,(String) prop.get(key));
            }
//            for (Map.Entry q:map.entrySet()) {
//                System.out.println(q.getKey());
//                System.out.println(q.getValue());
//            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
